package com.ctshk.rpc.order.tour.mapper;

import com.ctshk.rpc.order.tour.base.CustomBaseMapper;
import com.ctshk.rpc.order.tour.dto.custom.AddReceiptTouristDTO;
import com.ctshk.rpc.order.tour.dto.custom.ReceiptTouristDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.common.service.mapper.CtsBaseMapper;
import com.ctshk.rpc.order.tour.dto.SeriesOrderCollectionListDTO;
import com.ctshk.rpc.order.tour.entity.SeriesOrderCollection;
import com.ctshk.rpc.order.tour.req.SeriesOrderCollectionListReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderListReq;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 系列团游客收款 Mapper 接口
 * </p>
 *
 * @author 谭响
 * @since 2021-01-18
 */
public interface SeriesOrderCollectionMapper extends CustomBaseMapper<SeriesOrderCollection> {

    @SelectProvider(type = SeriesOrderCollectionProvider.class, method = "reciveListByTourist")
    List<SeriesOrderCollectionListDTO> reciveListByTourist(@Param("id") Long id);

    /**
     * 订单收款统计
     * @param collectionTypeOther
     * @param orderId
     * @param collectionTypeId
     * @return
     */
    @Select("SELECT COUNT(id) AS peopleNum,SUM(paid_in_amount) AS receiptAmount \n" +
            "FROM t_series_order_collection\n" +
            "WHERE order_id = #{order_id} AND collection_type_id != #{collection_type_id} AND collection_type_id != #{collection_type_other} AND `status` = 3 AND is_deleted = 0")
    AddReceiptTouristDTO queryAddDetailListByOrder(@Param("order_id") Long orderId, @Param("collection_type_id") Long collectionTypeId,@Param("collection_type_other") Long collectionTypeOther);

    /**
     * 旅客ID列表
     * @param orderId
     * @param collectionTypeId
     * @return
     */
    @Select("SELECT tourist_id,paid_in_amount \n" +
            "FROM t_series_order_collection\n" +
            "WHERE order_id = #{order_id} AND collection_type_id != #{collection_type_id} AND collection_type_id != #{collection_type_other} AND `status` = 3 AND is_deleted = 0")
    List<ReceiptTouristDTO> queryTouristIdByOrder(@Param("order_id") Long orderId, @Param("collection_type_id")Long collectionTypeId,@Param("collection_type_other") Long collectionTypeOther);

    /**
     * 批量更新收款记录（插入receiptId）
     * @param orderId
     * @param userId
     * @param collectionTypeId
     * @return
     */
    @Update("UPDATE t_series_order_collection\n" +
            "SET modified_id = #{modified_id},is_print = 1\n" +
            "WHERE order_id = #{order_id} AND collection_type_id != #{collection_type_id} AND is_deleted = 0")
    Integer batchUpdateReceipt(@Param("order_id") Long orderId, @Param("modified_id")Long userId,@Param("collection_type_id")Long collectionTypeId);

    /**
     * 批量修改是否打印收据状态
     * @param collectionIdList
     * @param userId
     */
    @Update("<script>"+"UPDATE t_series_order_collection\n" +
            "SET is_print = 0,modified_id = #{modified_id}\n" +
            "WHERE id IN "+
            "<foreach collection=\"list\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
            "#{id}\n" +
            "</foreach>"+
            "</script>")
    void updateByIdList(@Param("list") List<Long> collectionIdList, @Param("modified_id") Long userId);
}
